#ifndef _cap_afe_H_
#define _cap_afe_H_

#include "hal_conf.h"

#define Cref    12//接22pf参比电容，如此处为其他值，请修改
#define Cp      4//接2.4pf并联电容，如此处为其他值，请修改

#define I2C_ADDR     0x68
#define RESET_CMD    0X7A
#define RCNT_D	     0X5CD0
#define Fref_NO_DIV	 0x00

#define AVG_COUNT 20    //平均采样次数

typedef enum {

	OS_SD_Continuous_Trans       =0x00,  
	OS_SD_Stop_Trans             =0x01, 
	OS_SD_Single_Trans           =0x03,  
} MeasureMode_TypeDef;

typedef enum 
{
	In_CLK  = 0,
	Ex_CLK = 1
}clk_ch;

typedef enum 
{
	CH0  = 0,
	CH1 = 1
}MEA_CH;

typedef	enum
{

  DRIVE_I_200UA 	= 0x00,   
  DRIVE_I_400UA 	= 0x10,   
  DRIVE_I_800UA 	= 0x20,   
  DRIVE_I_1600UA	= 0x30,   
  DRIVE_I_2400UA  = 0x40,  
  DRIVE_I_3200UA  = 0x50   
}drive_i;

typedef	enum
{
	VT_GPIO_0		=GPIO_Pin_0,
	VT_GPIO_3		=GPIO_Pin_3,
	VT_GPIO_12	=GPIO_Pin_12,
	VT_GPIO_2		=GPIO_Pin_2,
	VT_GPIO_15 	=GPIO_Pin_15,

}ADC_GPIO;

typedef	enum
{
	VT_CH1		=ADC_Channel_1,
	VT_CH2		=ADC_Channel_2,
	VT_CH3		=ADC_Channel_3,
	VT_CH5		=ADC_Channel_5,
	VT_CH6 		=ADC_Channel_6,

}ADC_CH;

typedef struct
{
	ADC_CH 		vt_ch;
	ADC_GPIO 	vt_gpio;
	
	
}VT_CFG;

typedef struct
{
	u16 			count_ch0 ;
	u16 			count_ch1 ;
	clk_ch 		clk;
	drive_i 	I_DRIVE;
	u8				FIN_DIV;
	u8				FREF_DIV;
	
	u16				RCNT;
	float 		clkfre;
	float 		M_F;
	float 		R_F;
	float 		coef;
	float 		cap;
	float 		VT;
	VT_CFG 		VT_CH;
	float 		VDD;
	u16			avg;
	MEA_CH 		Measuring_channel;
	MEA_CH 		Reference_channel;
}CAP_AFE;


#define   REF_SEL_In_CLK     0x00<<7  
#define   REF_SEL_Ex_CLK     0x01<<7  

#define   INTB_EN_DIS     		   0x00<<6  
#define   INTB_EN_EN     	   0x01<<6  
#define   INTB_MODE_ALERT     	       0x00<<5  
#define   INTB_MODE_TRANS     	       0x01<<5  


#define   OS_SD_Continuous_Trans       0x00  
#define   OS_SD_Stop_Trans             0x01  
#define   OS_SD_Single_Trans           0x03  

#define   CH1_DISABLE               0x00<<7  
#define   CH1_ENABLE              0x01<<7  
#define   CH0_DISABLE               0x00<<6  
#define   CH0_ENABLE              0x01<<6  


//配置输入频率分频
#define  FIN_DIV_0        	 0x00  
#define  FIN_DIV_2        	 0x10   
#define  FIN_DIV_4        	 0x20 
#define  FIN_DIV_8        	 0x30 
#define  FIN_DIV_16         0x40   
#define  FIN_DIV_32         0x50   
#define  FIN_DIV_64         0x60   
#define  FIN_DIV_128        0x70  	
#define  FIN_DIV_256        0x80   

#define   VDD_SEL_L                    0x01   
#define   VDD_SEL_H                    0x00   

typedef enum 
{
  DATA_CH0_MSB								= 0x04,   
  DATA_CH0_LSB              	= 0x05,   
	
  DATA_CH1_MSB              	= 0x06,   
  DATA_CH1_LSB              	= 0x07,   
	
  RCNT_MSB              	= 0x0C, 
  RCNT_LSB	              = 0x0D,  
	
  SCNT               	    = 0x10,	  
	
  FIN_DIV                	= 0x15,	  
  FREF_DIV              	= 0x16,  

  STATUS            	    = 0x18,  

  TR_H             	        = 0x1D,	  
  TR_L                    	= 0x1E,	 
 
  CFG             	            = 0x1F,  
  CHX_EN                      	= 0x21,   
  MC_RESET                      = 0x22, 	
  DRIVE_I												= 0x25,   
  GLITCH_FILTER_EN							= 0x33,  
  CHIP_ID_MSB										= 0x7E,   
  CHIP_ID_LSB      			    		= 0x7F,    
  
} MC11S_REG;

/*******************************************************************
  * @brief  Capacitance correction.
  * @param  Cap_Structure.
  * @retval none.
 ******************************************************************/	
void DA1_DA0(CAP_AFE* Cap_Structure);

/*******************************************************************
  * @brief  initialization Registers.
  * @param  Cap_Structure.
  * @retval None.
 ******************************************************************/
void Registers_Init(CAP_AFE* Cap_Structure);

/*******************************************************************
  * @brief  Start Measurement.
  * @note  	none
  * @param  Cap_Structure.
  * @retval None.
 ******************************************************************/
void Single_Measure(CAP_AFE* Cap_Structure);

/***********************************************************************************************************************
  * @brief  Collect temperature information
  * @note   none
  * @param  Cap_Structure:CAP AFE information
  * @param  times:Average times
  * @retval none
  *********************************************************************************************************************/
void GetVT(CAP_AFE* Cap_Structure, u16 times);
#endif

